import weakref


class ExpensiveObject:

    def __init__(self, name):
        self.name = name

    def __del__(self):
        print('(Deleting {}'.format(self))


obj = ExpensiveObject('My object')
r = weakref.ref(obj)
p = weakref.proxy(obj)

print('via obj: ', obj.name)
print('via ref: ', r().name)
print('via proxy: ', p.name)

del obj
# 抛异常
print(print('via proxy: ', p.name))
